local UIShareHero           = UIBase.New(PanelNames.UIShareHero)

local m_heroData            = nil
local m_shareType           = nil
local m_drawWrapper         = nil
local m_modelId             = nil
local m_showSkinId          = nil
local m_equip_obj           = {}

function UIShareHero.Init(data)
    m_heroData = data.hero
    m_shareType = data.type
    m_showSkinId = data.skinId
    if m_shareType == EnumConst.ShareType.shareHero then
        m_modelId = tostring(m_heroData.partnerModelId)
    elseif m_shareType == EnumConst.ShareType.shareNewPlayer then
        m_modelId = tostring(data.heroId)
    else
        m_modelId = tostring(m_heroData.itemModelId)
    end

    logicMgr.ShareManager.SetShareType(m_shareType)

    UIBase.Init(UIShareHero)
end

function UIShareHero:OnCreate()
    -- utimer.delayCall(function()
    --     closeUI(PanelResNames.UIShareHero)
    -- end, 5000)
    
    self.quality        = self:GetChild("quality")
    self.quality_frame  = self.quality:GetChild("frame")
    self.quality_icon   = self.quality:GetChild("icon")
    self.quality_numicon= self.quality:GetChild("numIcon")
    self.ctr_jinjie     = self.quality:GetController("jinjie")
    self.ctr_LR         = self.quality:GetController("LR")
    self.ctr_quality    = self.quality:GetController("QualitySpine")


    self.txt_level      = self:GetChild("number")
    self.txt_heroName   = self:GetChild("name")
    self.txt_bannerTips = self:GetChild("bannerTips")
    self.txt_playerName = self:GetChild("playerName")
    self.txt_playerID   = self:GetChild("playerID")
    self.img_type       = self:GetChild("profession")
    self.root_head      = self:GetChild("playerHead")
    self.root_head      = self.root_head:GetChild("head")
    self.img_Icon       = self.root_head:GetChild("HeadIcon")
    self.img_Frame      = self.root_head:GetChild("HeadFrameIcon")
    self.draw           = self:GetChild("herolihui")
    self.inlaylink      = self:GetChild("inlaylink")
    self.frameItem      = self.inlaylink:GetChild("item")
    self.img_bg         = self:GetChild("share_bg02")
    self.equip_po1      = self:GetChild("HandPos")
    self.equip_po2      = self:GetChild("HeadPos")
    self.equip_po3      = self:GetChild("BodyPos")
    self.equip_po4      = self:GetChild("LegPos")
    self.txt_lbs        = self:GetChild("LBS")
    self.ctr_sex        = self:GetController("Gender")

    self.ctr_sex        = logicMgr.HeroManager.GetRoleInfo("sex")
    self.ctr_state      = self:GetController("state")
    self.ctr_showQRcode = self:GetController("showRWM")
    self.img_qrcode     = self:GetChild("PicShare")
    self.ctr_showQRcode.selectedIndex = 0

    -- breakStone = UIBreakStone:new(self.quality);
    if self.ctr_showQRcode.selectedIndex == 0 then
        if m_shareType == EnumConst.ShareType.shareNewPlayer then
            --ShareManager.GetQrcodeUrl(function(url)
                utimer.delayCall(function()
                    local code = logicMgr.NewPlayerActiveManager.GetInviteCode()
                    local url = "https://test-fszxshare.xiaoniuhy.com/?code="..code
                    self.img_qrcode.texture = NTexture.New(Util.CreateQRCode(url))
                end, 500)
            --end,"https://yxzmadvertisingdouyu.xiaoniuhy.com/yxzm_index.html")
        else
            ShareManager.GetQrcodeUrl(function(url)
                utimer.delayCall(function()
                    self.img_qrcode.texture = NTexture.New(Util.CreateQRCode(url))
                end, 500)
            end)
        end
    end

    m_equip_obj[1]      = self.equip_po1
    m_equip_obj[2]      = self.equip_po2
    m_equip_obj[3]      = self.equip_po3
    m_equip_obj[4]      = self.equip_po4

    self:GetController("type").selectedIndex = m_shareType == EnumConst.ShareType.shareHero and 0 or 1

    -- m_drawWrapper = { GoWrapper.New(), GoWrapper.New() }
     m_drawWrapper       = GoWrapper.New()

    self:ShowHeroSpine()
    self:BindHero()

    self:BindPlayer()

    closeUI(PanelResNames.UIScreenEffect)
    utimer.delayCall(function()
        LuaHelper.GetGameManager():ShareTexture(function(tex)
            if m_shareType == 3 then
                showUI(PanelResNames.UIShare, 2, nil)
            else
                showUI(PanelResNames.UIShare, 1, m_modelId)
            end
            showUI(PanelResNames.UIScreenEffect)
        end)
    end, 700)
end

function UIShareHero:BindDraw()
    
end

function UIShareHero:BindHero()
    local config = G.dataTable["t_hero"][m_modelId]
    if config then
        self.txt_heroName.text = getLanguage(config.f_HeroName)
        self.img_type.url ="ui://ICONAtlas/".. EnumConst.typeName[config["f_HeroType"]]

        if m_shareType == EnumConst.ShareType.shareHero then
            self:SetTips(1)
        else
            if config.f_HeroInitialStar >= 60 then
                if config.f_HeroRace == 1 or config.f_HeroRace == 2 then
                    self:SetTips(6)
                else
                    self:SetTips(5)
                end
            end
        end
    end

    if m_shareType == EnumConst.ShareType.shareHero then
        local equips = logicMgr.EquipManager.getHeroEquipList(m_heroData.partnerId)
        local ctrItem = CtrlManager.GetCtrl(PanelNames.EquipItem); 
        local equipnum = 0
        for k,v in pairs(equips) do
            if ctrItem  then
                ctrItem.initData(m_equip_obj[v.part], v)
                equipnum = equipnum + 1
            end
        end

        local equip = true
        if equipnum < 4 then
            -- for i=1, table.getn(m_equip_obj) do
            --     m_equip_obj[i].visible = false
            -- end
            equip = false
        end

        local itemId = StrangeFragmentManager.GetCurrentEquipFragment(m_heroData.partnerId)
        if itemId then
            self.inlaylink.visible = true
            local item = CtrlManager.GetCtrl(PanelNames.Item);
            if item ~= nil then
                StrangeFragmentManager.SetItemData(self.frameItem, itemId, false)
            end
            if equip then
                self.ctr_state.selectedIndex = 0
            else
                self.ctr_state.selectedIndex = 3
            end
        else
            if equip then
                self.ctr_state.selectedIndex = 2
            else
                self.ctr_state.selectedIndex = 1
            end
            -- self.inlaylink.visible = false
        end
        self.txt_level.text = logicMgr.HeroManager.getHeroShowLevelByPartnerId(m_heroData.partnerId) --m_heroData.level 
        

        ----quality
        local star = logicMgr.HeroManager.starToQuality(m_heroData.star)
        -- if m_heroData.star >= 100 then
        --     breakCtrl.selectedIndex=1
        -- else
        --     breakCtrl.selectedIndex=0
        -- end
        local config = G.dataTable.t_iconframe[tostring(star)]
        local isFrame = config.f_Ornaments ~= ""
        self.quality_frame.visible = isFrame
        local atlas = "ui://CharactorUI/"
        if isFrame then
            self.quality_frame.url = atlas .. config.f_QualityOrnaments
        end
        if config.f_QualityText ~= "" then
            self.quality_icon.url = atlas .. config.f_QualityText
            self.ctr_jinjie .selectedIndex = 1;
        else
            self.ctr_jinjie.selectedIndex = 0;
        end
        local quality = tonumber(config.f_Quality);
        -- breakStone:setQuality(quality);
        self.quality_numicon.url = atlas .. config.f_QualityIcon
        if config.f_Id > 10 then
            self.ctr_jinjie.selectedIndex = 0;
            self.ctr_LR.selectedIndex = star - 10;
            --maxController.selectedIndex = 0;
        else
            self.ctr_LR.selectedIndex = 0;
        end
        ----quality

        self.img_bg.url = "ui://6bi6w5bxcfgvozqg"
        -- if m_heroData.star > 
        -- self.ctr_quality.selectedIndex = 2

        -- self.ctr_jinjie     = self.quality:GetController("jinjie")
        -- self.ctr_LR         = self.quality:GetController("LR")
        -- self.ctr_quality    = self.quality:GetController("QualitySpine")
    end
    if m_shareType == EnumConst.ShareType.shareNewPlayer then
        self:GetController("type").selectedIndex = 0
        self.ctr_state.selectedIndex = 4
        local initStar = G.dataTable["t_hero"][m_modelId].f_HeroInitialStar
        local star = logicMgr.HeroManager.starToQuality(initStar)
        local config = G.dataTable.t_iconframe[tostring(star)]
        local isFrame = config.f_Ornaments ~= ""
        self.quality_frame.visible = isFrame
        local atlas = "ui://CharactorUI/"
        if isFrame then
            self.quality_frame.url = atlas .. config.f_QualityOrnaments
        end
        if config.f_QualityText ~= "" then
            self.quality_icon.url = atlas .. config.f_QualityText
            self.ctr_jinjie .selectedIndex = 1;
        else
            self.ctr_jinjie.selectedIndex = 0;
        end
        self.quality_numicon.url = atlas .. config.f_QualityIcon
        if config.f_Id > 10 then
            self.ctr_jinjie.selectedIndex = 0;
            self.ctr_LR.selectedIndex = star - 10;
        else
            self.ctr_LR.selectedIndex = 0;
        end
    end
end

--显示立绘
function UIShareHero:ShowHeroSpine()
    local useSkinId = m_showSkinId or logicMgr.SkinManager.getHeroSkinId(m_modelId)
    --local path = HeroManager.GetHeroDrawingRolePath(m_modelId, nil, useSkinId)
    local path = logicMgr.SkinManager.GetHeroDrawPathBySkinId(useSkinId)--HeroManager.GetHeroDrawingPath(m_modelId, nil, useSkinId)
    
    resMgr:LoadModelPrefab(path, function(go)
        local model = newObject(go);
        model:SetActive(true); 
        model:AddComponent(TCanvas);
        model:AddComponent(TChangeSortingOrder);
        m_drawWrapper:SetWrapTarget(model, true)
        -- local scale,pos = HeroManager.GetHeroLocationResize(m_modelId)

        local scale, pos = logicMgr.HeroManager.GetHeroLocationResizeBySkinId(useSkinId)
        -- local tempPos = Vector3.New(location[3], location[4], 0);
       
        Util.Lua_SetLocalPositionXYZ(model.transform,pos[1],pos[2],0)
        Util.Lua_SetLocalScaleXYZ(model.transform,scale[1],scale[2],0)
        self.draw:SetNativeObject(m_drawWrapper)
        local g = model:GetComponent("SkeletonGraphic")
        m_graphic = g
        g.AnimationState:SetAnimation(0,"idle1",true);
    end)
end

function UIShareHero:BindPlayer()
    self.txt_playerID.text = string.format(getLanguage("SharePlayerID"), logicMgr.HeroManager.GetRoleInfo().playerId) 
    self.txt_playerName.text = G.serverData["roleInfo"].show.name
    self.txt_lbs.text = logicMgr.LBSManager.GetLocationName()

    local myself = logicMgr.HeroManager.GetRoleInfo();
    if myself ~= nil then
        local _roleName = logicMgr.HeroManager.GetRoleName()
        local level = logicMgr.HeroManager.GetRoleLevel()

        self.img_Icon.url = logicMgr.SkinManager.GetHeroIconBySkinId(logicMgr.HeroManager.GetRoleInfo("pic"))
        local itemid = logicMgr.HeroManager.GetRoleInfo("picFrame")--frameId or logicMgr.HeroManager.GetRoleInfo("picFrame")
        self.img_Frame.url = EnumConst.AtlasPath.ICONAtlas .. itemid;
    end
end

function UIShareHero:SetTips(type)
    if m_shareType == 1 then
        self.txt_bannerTips.text = getLanguage("ShareHeroTips1")
        
    else
        self.txt_bannerTips.text = getLanguage("ShareDrawTips" .. type)
        TAManager.TrackByDictionary("share_success", {language = "ShareDrawTips" .. type})
    end
end

function UIShareHero:ReqShareReward()
    if m_shareType == EnumConst.ShareType.shareHero then
        -- local info = HeroManager.getHandbookByModelId(m_modelId, true)
        -- if info.share == 0 then
        --     info.share = 1
        --     --partnerHandler.ReqShareHeroReward(tonumber(m_modelId))
        -- end
        -- TAManager.TrackByDictionary("share_hero", {heroid = m_modelId})
        ShareManager.Req_shareReward()
    else
        ShareManager.Req_shareReward()
    end
end

return UIShareHero